<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>注册-江城巴士</title>
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="favicon.ico">
    
    <link rel="stylesheet" href="css/app.css">
</head>

<body class="bg-reg">

<header>
    <div class="title">注册</div>
</header>

<section class="wrap reg-box">
    <form action="">
        <div class="form">
            <div class="item">
                <label class="for">手机号</label>
                <input type="text" class="ipt" value="" name="" id="mobile" placeholder="请输入手机号" />
                <span class="error"></span>
            </div>

            <div class="item">
                <label class="for">验证码</label>
                <input type="text" class="ipt" name="" id="code" placeholder="请输入验证码" maxlength="6" />
                <span class="error"></span>
                <span class="btn-wrap"><button type="button" class="btn-code" id="getcode">获取验证码</button></span>
            </div>

            <div class="item">
                <label><input type="checkbox" checked class="cbx">我已阅读并同意xxx的<a href="agreement.html" class="agreement" target="_blank">服务条款</a></label>
            </div>
        </div>

        <div class="button">
            <button type="submit" class="ubtn ubtn-blue" id="submit">注册</button>
        </div>
    </form>
</section>


<script src="js/zepto.min.js"></script>
<script src="js/app.js"></script>
<script>
$(function() {
    var $mobile = $('#mobile'),
        $code = $('#code');

    function checkMobile() {
        var val = $mobile.val();
        if (!val) {
            $mobile.next().html('请输入手机号').show();
        } else if (!/^1[3-9]\d{9}$/.test(val)) {
            $mobile.next().html('手机号码格式错误').show();
        } else {
            $mobile.next().html('').hide();
            return true;
        }
        $mobile.focus();
        return false;
    }
    function checkCode() {
        var val = $code.val();
        if (!val) {
            $code.next().html('请输入验证码').show();
        } else {
            $code.next().html('').hide();
            return true;
        }
        $code.focus();
        return false;
    }
    function checkIpt() {
        if (checkMobile() && checkCode()) {
            return true;
        }
        return false;
    }
    // 保存
    $('#submit').on('click', function() {
        return checkIpt();
    });

    $('body').on('click', function() {
        $('.error').html('').hide();
    });

    $mobile.on('input', function() {
        $(this).next().html('').hide();
    });

    var $getcode = $("#getcode");

    // 短信验证码
    var sendSMS = {
        timer: 0,
        delay: false, //间隔时间，单位：秒
        text: '秒重新获取',
        send: function(time) {
            var self = this;
            if (self.delay) {
                return;
            }
            $.ajax({
                url: '',
                // dataType: 'json',
                data: 'phone=' + $mobile.val(),
                // type: 'post',
                success: function(data) {
                    data = {
                        status: 'success'
                    }
                    if (data.status === 'success') {
                        self.lock(time);
                        alertMsg(data.message);
                    }else{
                        alertMsg(data.message);
                    }
                },
                error: function() {
                    alertMsg('网络连接超时，请您稍后重试!');
                }
            })
        },
        lock: function(time) {
            var self = this;
            self.delay = true;
            self.timer && clearInterval(self.timer);

            $getcode.text(time + self.text).prop('disabled', true);

            self.timer = setInterval(function() {
                $getcode.text(--time + self.text).prop('disabled', true);
                if (time <= 0) {
                    clearInterval(self.timer);
                    $getcode.text("获取验证码").prop('disabled', false);
                    self.delay = false;
                }
            }, 1e3);
        }
    }

    $getcode.on("click", function(e) {
        e.stopPropagation();
        checkMobile() && sendSMS.send(60);
    })
});

</script>

</body>
</html>